package ca.ualberta.cs.PrivacyManagement;

import javax.mail.*;
import javax.mail.internet.*;

import ca.ualberta.cs.CLRDB.User;

import java.util.Properties;


public class Notifications {

	private static String from = "coraladmn@gmail.com";
    private static String password = "fab4coral";
    private static String host = "smtp.gmail.com";
	
	public static String generatePasswordChangedMessage(String pass){
		return "Your password for the CoRAL Fab4browser is now changed to:" +
			"\n " + pass +
			"\n " + "due to your request.";
	}
	
	/**
	 * sends a friendship request notification to requested, includes information about the requester
	 * @param requester
	 * @param toEmail
	 * @param requestedId
	 * @return 1: ok, 0: failed
	 */
	public static int sendFriendshipRequest(User requester, String toEmail, int requestedId){
		
		String msg = "You have received a friendship request from:<br/><br/>" +
				"<b>Username:</b> "+requester.getUsername()+"<br/>" +
				"<b>Name:</b> "+(requester.getName()!=null?requester.getName():"Not provided")+"<br/>" +
				"<b>Affiliation:</b> "+(requester.getAffiliation()!=null?requester.getAffiliation():"Not provided")+"<br/>" +
				"<b>Description:</b> "+(requester.getDescription()!=null?requester.getDescription():"Not provided")+"<br/>" +
				"<br/>" +
				"Please go to your <a href=\"http://hypatia.cs.ualberta.ca/CoRAL/dev/trunk/person_view.php?id="+requestedId+"\">profile</a> and accept/reject this request.<br/>" +
				"http://hypatia.cs.ualberta.ca/CoRAL/dev/trunk/person_view.php?id="+requestedId+"" ;
				//TODO: trunk should be removed
				
				System.out.println("message:"+msg);
		return emailNotification(msg, "CoRAL: new friendship request", toEmail);
	}
	
	/**
	 * sends a friendship request notification to requested, includes information about the requester
	 * @param requester
	 * @param toEmail
	 * @return 1: ok, 0: failed
	 */
	public static int sendNewFollowerInfo(User requester, String toEmail){
		
		String msg = "This person is now following you in CoRAL:<br/><br/>" +
				"<b>Username:</b> "+requester.getUsername()+"<br/>" +
				"<b>Name:</b> "+(requester.getName()!=null?requester.getName():"Not provided")+"<br/>" +
				"<b>Affiliation:</b> "+(requester.getAffiliation()!=null?requester.getAffiliation():"Not provided")+"<br/>" +
				"<b>Description:</b> "+(requester.getDescription()!=null?requester.getDescription():"Not provided")+"<br/>" +
				"<br/>";
								
				System.out.println("message:"+msg);
		return emailNotification(msg, "CoRAL: new friendship request", toEmail);
	}
	
	/**
	 * sends a message to the given email from coral's admin email address
	 * @param messageBody
	 * @param messageTitle
	 * @param email
	 * @return 1: ok, 0: failed
	 */
	private static int emailNotification(String messageBody, String messageTitle, String email){
		try{
			Properties props = new Properties();
			props.put("mail.smtp.host", "smtp.gmail.com");
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.port", "587");
			props.put("mail.smtp.socketFactory.port" , "587");
			props.put("mail.smtp.starttls.enable" , "true");

			Session s = Session.getInstance(props, null);
			s.setDebug(false);

			MimeMessage msg = new MimeMessage(s);

			String fromEmail = " "+from+" ";

			String addressTo = " "+email+" ";
			InternetAddress to = new InternetAddress(addressTo);

			InternetAddress fromAddr = new InternetAddress(fromEmail);

			msg.setSentDate(new java.util.Date());
			msg.setFrom(fromAddr);
			msg.addRecipient(Message.RecipientType.TO, to);

			msg.setSubject(messageTitle);
			msg.setContent(messageBody,"text/html");
			//TODO: try it for text/html.

			Transport tr = s.getTransport("smtp");
			String smtp_server = props.getProperty("mail.smtp.host");

			tr.connect(smtp_server, from, password);
			msg.saveChanges();
			tr.sendMessage(msg, msg.getAllRecipients());
			tr.close();

		}
		catch(Exception e){
			e.printStackTrace();
			return 0;
		}
		return 1;
	}
	
/*	public static void main(String[] args) throws Exception{
	      Properties props = new Properties();
	      
	      
	      props.setProperty("mail.transport.protocol", "smtp");
//	      props.setProperty("mail.host", "smtp.srv.ualberta.ca");
	      
	      props.put("mail.smtp.port", Integer.toString(465));
	      props.setProperty("mail.host", "smtp.gmail.com");
	      props.put("mail.smtps.auth", "true");
	      
	      props.setProperty("mail.user", username);
	      props.setProperty("mail.password", pass);

	      Session mailSession = Session.getDefaultInstance(props, null);
	      Transport transport = mailSession.getTransport();

	      MimeMessage message = new MimeMessage(mailSession);
	      message.setSubject("Testing javamail plain");
	      message.setContent("This is a test", "text/plain");
	      message.addRecipient(Message.RecipientType.TO,
	           new InternetAddress("samaneh@ualberta.ca"));

	      transport.connect(host, username, pass);
	      transport.sendMessage(message,
	          message.getRecipients(Message.RecipientType.TO));
	      transport.close();
	    }*/

	public static void main(String[] args){
		
	}
}
